public class GenericConversionService extends java.lang.Object implements ConfigurableConversionService
ConversionService
implementation suitable for use in most environments.
Indirectly implements ConverterRegistry
as registration API through the
ConfigurableConversionService
interface.Modifier and Type | Class and Description |
---|---|
private class |
GenericConversionService.ConverterAdapter |
private static class |
GenericConversionService.ConverterCacheKey |
private class |
GenericConversionService.ConverterFactoryAdapter |
private static class |
GenericConversionService.MatchableConverters |
Modifier and Type | Field and Description |
---|---|
private java.util.Map<GenericConversionService.ConverterCacheKey,GenericConverter> |
converterCache |
private java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters>> |
converters |
private static GenericConverter |
NO_MATCH |
private static GenericConverter |
NO_OP_CONVERTER |
Constructor and Description |
---|
GenericConversionService() |
Modifier and Type | Method and Description |
---|---|
void |
addConverter(java.lang.Class<?> sourceType,
java.lang.Class<?> targetType,
Converter<?,?> converter)
Add a plain converter to this registry.
|
void |
addConverter(Converter<?,?> converter)
Add a plain converter to this registry.
|
void |
addConverter(GenericConverter converter)
Add a generic converter to this registry.
|
void |
addConverterFactory(ConverterFactory<?,?> converterFactory)
Add a ranged converter factory to this registry.
|
private void |
addInterfaceHierarchy(java.lang.Class<?> interfaceType,
java.util.Set<java.lang.Class<?>> interfaces) |
private void |
assertNotPrimitiveTargetType(TypeDescriptor sourceType,
TypeDescriptor targetType) |
boolean |
canConvert(java.lang.Class<?> sourceType,
java.lang.Class<?> targetType)
Returns true if objects of sourceType can be converted to targetType.
|
boolean |
canConvert(TypeDescriptor sourceType,
TypeDescriptor targetType)
Returns true if objects of sourceType can be converted to the targetType.
|
<T> T |
convert(java.lang.Object source,
java.lang.Class<T> targetType)
Convert the source to targetType.
|
java.lang.Object |
convert(java.lang.Object source,
TypeDescriptor targetType)
Convenience operation for converting a source object to the specified targetType, where the targetType is a descriptor that provides additional conversion context.
|
java.lang.Object |
convert(java.lang.Object source,
TypeDescriptor sourceType,
TypeDescriptor targetType)
Convert the source to targetType.
|
protected java.lang.Object |
convertNullSource(TypeDescriptor sourceType,
TypeDescriptor targetType)
Template method to convert a null source.
|
private GenericConverter |
findConverterForClassPair(TypeDescriptor sourceType,
TypeDescriptor targetType) |
protected GenericConverter |
getConverter(TypeDescriptor sourceType,
TypeDescriptor targetType)
Hook method to lookup the converter for a given sourceType/targetType pair.
|
protected GenericConverter |
getDefaultConverter(TypeDescriptor sourceType,
TypeDescriptor targetType)
Return the default converter if no converter is found for the given sourceType/targetType pair.
|
private GenericConversionService.MatchableConverters |
getMatchableConverters(java.lang.Class<?> sourceType,
java.lang.Class<?> targetType) |
private GenericConverter |
getMatchingConverterForTarget(TypeDescriptor sourceType,
TypeDescriptor targetType,
java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> converters) |
private GenericConverter.ConvertiblePair |
getRequiredTypeInfo(java.lang.Object converter,
java.lang.Class<?> genericIfc) |
private java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> |
getSourceConverterMap(java.lang.Class<?> sourceType) |
private java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> |
getTargetConvertersForSource(java.lang.Class<?> sourceType) |
private java.lang.Object |
handleConverterNotFound(java.lang.Object source,
TypeDescriptor sourceType,
TypeDescriptor targetType) |
private java.lang.Object |
handleResult(TypeDescriptor sourceType,
TypeDescriptor targetType,
java.lang.Object result) |
private void |
invalidateCache() |
private GenericConverter |
matchConverter(GenericConversionService.MatchableConverters matchable,
TypeDescriptor sourceFieldType,
TypeDescriptor targetFieldType) |
void |
removeConvertible(java.lang.Class<?> sourceType,
java.lang.Class<?> targetType)
Remove any converters from sourceType to targetType.
|
java.lang.String |
toString() |
private static final GenericConverter NO_OP_CONVERTER
private static final GenericConverter NO_MATCH
private final java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters>> converters
private final java.util.Map<GenericConversionService.ConverterCacheKey,GenericConverter> converterCache
public void addConverter(Converter<?,?> converter)
ConverterRegistry
addConverter
in interface ConverterRegistry
public void addConverter(java.lang.Class<?> sourceType, java.lang.Class<?> targetType, Converter<?,?> converter)
ConverterRegistry
addConverter
in interface ConverterRegistry
public void addConverter(GenericConverter converter)
ConverterRegistry
addConverter
in interface ConverterRegistry
public void addConverterFactory(ConverterFactory<?,?> converterFactory)
ConverterRegistry
addConverterFactory
in interface ConverterRegistry
public void removeConvertible(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
ConverterRegistry
removeConvertible
in interface ConverterRegistry
sourceType
- the source typetargetType
- the target typepublic boolean canConvert(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
ConversionService
ConversionService.convert(Object, Class)
is capable of converting an instance of sourceType to targetType.
Special note on collections, arrays, and maps types:
For conversion between collection, array, and map types, this method will return 'true'
even though a convert invocation may still generate a ConversionException
if the underlying elements are not convertible.
Callers are expected to handle this exceptional case when working with collections and maps.canConvert
in interface ConversionService
sourceType
- the source type to convert from (may be null if source is null)targetType
- the target type to convert to (required)public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
ConversionService
ConversionService.convert(Object, TypeDescriptor, TypeDescriptor)
is capable of converting an instance of sourceType to targetType.
Special note on collections, arrays, and maps types:
For conversion between collection, array, and map types, this method will return 'true'
even though a convert invocation may still generate a ConversionException
if the underlying elements are not convertible.
Callers are expected to handle this exceptional case when working with collections and maps.canConvert
in interface ConversionService
sourceType
- context about the source type to convert from (may be null if source is null)targetType
- context about the target type to convert to (required)public <T> T convert(java.lang.Object source, java.lang.Class<T> targetType)
ConversionService
convert
in interface ConversionService
source
- the source object to convert (may be null)targetType
- the target type to convert to (required)public java.lang.Object convert(java.lang.Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
ConversionService
convert
in interface ConversionService
source
- the source object to convert (may be null)sourceType
- context about the source type converting from (may be null if source is null)targetType
- context about the target type to convert to (required)targetType
public java.lang.Object convert(java.lang.Object source, TypeDescriptor targetType)
convert(Object, TypeDescriptor, TypeDescriptor)
and encapsulates the construction of the sourceType descriptor using TypeDescriptor.forObject(Object)
.source
- the source objecttargetType
- the target typeConversionException
- if a conversion exception occurredjava.lang.IllegalArgumentException
- if targetType is nulljava.lang.IllegalArgumentException
- if sourceType is null but source is not nullpublic java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType)
Default implementation returns null
.
Subclasses may override to return custom null objects for specific target types.
sourceType
- the sourceType to convert fromtargetType
- the targetType to convert toprotected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
sourceType
- the source type to convert fromtargetType
- the target type to convert tonull
if no suitable converter was foundgetDefaultConverter(TypeDescriptor, TypeDescriptor)
protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
null
otherwise, indicating no suitable converter could be found.
Subclasses may override.sourceType
- the source type to convert fromtargetType
- the target type to convert toprivate GenericConverter.ConvertiblePair getRequiredTypeInfo(java.lang.Object converter, java.lang.Class<?> genericIfc)
private GenericConversionService.MatchableConverters getMatchableConverters(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
private void invalidateCache()
private java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> getSourceConverterMap(java.lang.Class<?> sourceType)
private GenericConverter findConverterForClassPair(TypeDescriptor sourceType, TypeDescriptor targetType)
private java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> getTargetConvertersForSource(java.lang.Class<?> sourceType)
private GenericConverter getMatchingConverterForTarget(TypeDescriptor sourceType, TypeDescriptor targetType, java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> converters)
private void addInterfaceHierarchy(java.lang.Class<?> interfaceType, java.util.Set<java.lang.Class<?>> interfaces)
private GenericConverter matchConverter(GenericConversionService.MatchableConverters matchable, TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType)
private java.lang.Object handleConverterNotFound(java.lang.Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
private java.lang.Object handleResult(TypeDescriptor sourceType, TypeDescriptor targetType, java.lang.Object result)
private void assertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescriptor targetType)